package cs3343.project.family.UI;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.ConfigManager;
import cs3343.project.family.GameController;

/**
 * The Class WelcomeFrame.
 */
public class WelcomeFrame extends JFrame implements ActionListener {
	
	/** The gc. */
	private GameController gc;

	/** The content pane. */
	private JPanel contentPane;
	
	/** The full panel. */
	private JPanel fullPanel;
	
	/** The cover panel. */
	private JPanel coverPanel;
	
	/** The btn panel. */
	private JPanel btnPanel;
	
	/** The cover img. */
	private JLabel coverImg;
	
	/** The btn game start. */
	private JButton btnGameStart;
	
	/** The btn game setting. */
	private JButton btnGameSetting;
	
	/** The btn game create card. */
	private JButton btnGameCreateCard;
	
	/** The btn game statistic. */
	private JButton btnGameStatistic;

	/**
	 * Create the frame.
	 *
	 * @param gc the gc
	 */
	public WelcomeFrame(GameController gc) {
		this.gc = gc;
		
		initUI();
		
		btnGameStart = new JButton(ConfigManager.getLangString("btnGameStart"));
		btnGameSetting = new JButton(ConfigManager.getLangString("btnGameSetting"));
		btnGameCreateCard = new JButton(ConfigManager.getLangString("btnGameCreateCard"));
		
		coverImg = new JLabel(new ImageIcon(ConfigManager.IMAGE_PATH + "family.jpg"));
		
		addButton(btnGameStart, btnPanel);
		addButton(btnGameSetting, btnPanel);
		addButton(btnGameCreateCard, btnPanel);
		
		fullPanel.add(coverImg, BorderLayout.NORTH);
		fullPanel.add(btnPanel, BorderLayout.SOUTH);
		
		btnGameStart.addActionListener(this);
		btnGameSetting.addActionListener(this);
		btnGameCreateCard.addActionListener(this);
		
		gc.getUiController().frameCentering(this);
	}
	
	/**
	 * Adds the button.
	 *
	 * @param btn the btn
	 * @param container the container
	 */
	private void addButton(JButton btn, Container container) {
		btn.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(btn);
    }
	
	/**
	 * Inits the ui.
	 */
	private void initUI() {
		setTitle(ConfigManager.getLangString("welcomeFrameTitle"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		fullPanel = new JPanel();
		coverPanel = new JPanel();
		btnPanel = new JPanel();
		
		fullPanel.setLayout(new BorderLayout());
		coverPanel.setLayout(new BorderLayout());		
		
		contentPane.add(fullPanel);
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnGameStart) {
			gc.getUiController().showCharacterFrame();
			this.dispose();
		}
		else if (e.getSource() == btnGameSetting) {
			gc.getUiController().showGameSettingFrame();
			this.dispose();
		}
		else if (e.getSource() == btnGameCreateCard) {
			gc.getUiController().showCreateCardFrame();
			this.dispose();
		}
	}
}
